﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CIIS.Common;
using Contract.Domain;
using Framework.Domain;
using DevExpress.XtraEditors;
using DevExpress.XtraLayout.Utils;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraEditors.Repository;
using Framework;
using System.Collections;
namespace CIIS
{
    public partial class Arrangement : BaseForm
    {
        t_tq_gzbz gzbz
        {
            get { return BindEntity as t_tq_gzbz; }
        }

        public Arrangement()
        {
            InitializeComponent();

            normalSignatureBzsx.InitControl("工作布置事项：", "使用民警：", "时间：", dao.SymjDcp);
            normalSignatureGzsh.InitControl("工作审核人意见：", "审核人签名：", "时间：", dao.UserDcp);
            gridSignatureWcrwqk.InitControl("完成任务情况：", "使用民警：", "时间：", dao.SymjDcp);

            dateEditBzsj.DateTime = DateTime.Today;

            DevControlUtils.Instance.InitLookUpEdit(lookUpEditTqry,dao.TqryDcp);

            buttonSave.Click += new EventHandler(buttonSave_Click);
            buttonSave.DialogResult = DialogResult.OK;
            buttonBack.Click += new EventHandler(buttonBack_Click);
        }

        void buttonSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (OptType == EOperateType.Add)
                {
                    BindEntity.State.MarkNew();
                }
                else
                {
                    BindEntity.State.MarkDirty();
                }
                gzbz.BZSJ = dateEditBzsj.DateTime;

                gzbz.BZSX = normalSignatureBzsx.Yj;
                gzbz.BZSX_SYMJ = normalSignatureBzsx.Qz;
                gzbz.BZSX_SYSJ = normalSignatureBzsx.Qzsj;

                gzbz.GZSH_YJ = normalSignatureGzsh.Yj;
                gzbz.GZSH_SHR = normalSignatureGzsh.Qz;
                gzbz.GZSH_SHSJ = normalSignatureGzsh.Qzsj;

                gzbz.WCRW_SYMJ = gridSignatureWcrwqk.Qz;
                gzbz.WCRW_WCSJ = gridSignatureWcrwqk.Qzsj;

                t_tq_gzbz o = dao.Dao.SaveOrUpdate(BindEntity) as t_tq_gzbz;

                gridSignatureWcrwqk.Gc.SaveData();

                XtraMessageBox.Show("保存成功");
            }
            catch
            {
                XtraMessageBox.Show("保存失败");
            }
            finally
            {
                Close();
            }
        }

        void buttonBack_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void lookUpEditTqry_EditValueChanged(object sender, EventArgs e)
        {
            textEditRydh.Text = lookUpEditTqry.EditValue.ToString();

            gridSignatureWcrwqk.Gc.KeyValue = lookUpEditTqry.EditValue.ToString();
        }

        private void InitControls()
        {
            if (OptType == EOperateType.Detail)
            {
                layoutControlItemSave.Visibility = LayoutVisibility.Never;
            }
        }

        /// <summary>
        /// 绑定数据源，在show窗口之前需要调用
        /// </summary>
        /// <param name="tqry"></param>
        public override void BindData(Entity entity)
        {
            InitControls();

            BindEntity = entity as t_tq_gzbz;
            if (BindEntity == null)
            {
                BindEntity = new t_tq_gzbz();
                gzbz.State.MarkNew();
                gzbz.Id = Guid.NewGuid().ToString().Replace("-", string.Empty);
            }

            if (gzbz != null)
            {
                textEditRydh.DataBindings.Add("Text", gzbz, "RYDH");

                lookUpEditBzdd.DataBindings.Add("EditValue", gzbz, "BZDD");
                lookUpEditTqry.DataBindings.Add("EditValue", gzbz, "RYDH");

                normalSignatureGzsh.SetValues(new object[] { gzbz.GZSH_YJ, gzbz.GZSH_SHR, gzbz.GZSH_SHSJ });
                normalSignatureBzsx.SetValues(new object[] { gzbz.BZSX, gzbz.BZSX_SYMJ, gzbz.BZSX_SYSJ });
                gridSignatureWcrwqk.SetValues(new object[] { gzbz.WCRW_SYMJ, gzbz.WCRW_WCSJ });

                if (gzbz.BZSJ.HasValue)
                {
                    dateEditBzsj.DateTime = gzbz.BZSJ.Value;
                }

                gridSignatureWcrwqk.Gc.DataType = typeof(t_xs_ccjg);
                gridSignatureWcrwqk.Gc.KeyString = "TXR_DH";
                gridSignatureWcrwqk.Gc.KeyValue = gzbz.RYDH;
                gridSignatureWcrwqk.Gc.InitColumns(
                    new string[] { "线索编号", "提供人", "提供时间", "线索破案情况" },
                    new string[] { "XSBH", "TXR_XM", "TXSJ", "PAJK" });
                //GridColumn gc = gridSignatureWcrwqk.Gc.Gv.Columns["XSBH"];
                //RepositoryItemLookUpEdit rilue = new RepositoryItemLookUpEdit();
                //DevControlUtils.Instance.InitRepositoryItemLookUpEdit(rilue,
                //    new DropdownControlParam() { ValueMember = "", DisplayMember = "", DataSource = dao.GetListFromCache<t_xs_xsda>() });

                QueryInfo info = new QueryInfo();
                info.QueryObject = "t_xs_ccjg";
                info.AddParam("TXR_DH", string.IsNullOrWhiteSpace(gzbz.RYDH) ? "-1" : gzbz.RYDH);
                IList gx = dao.Dao.FindList(info);
                if (gx.Count == 0)
                {
                    gx.Add(new t_xs_ccjg() { TXR_DH = gzbz.RYDH });
                }
                //DataTable table = Utils.Instance.ToDataTable(gx);
                gridSignatureWcrwqk.Gc.DataSource = gx;
                gridSignatureWcrwqk.Gc.RefreshDataSource();
            }
        }
    }
}
